package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xn.app.muying.R;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.NetworkUtil;
import com.xn.app.muying.util.ToastUtil;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 选择孕期状态
 * Created by Administrator on 2016/10/19.
 */
public class YunQiXuanZeActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private String result;
    private int id;
    private String pageIntent;
    //数据库
    private DBManager db = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yunqixuanze_activity);
        db = new DBManager(this);
        ImageView back = (ImageView) findViewById(R.id.yunqixuanze_activity_back);
        btn1 = (Button) findViewById(R.id.yunqixuanze_activity_btn1);
        btn2 = (Button) findViewById(R.id.yunqixuanze_activity_btn2);
        btn3 = (Button) findViewById(R.id.yunqixuanze_activity_btn3);
        back.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        pageIntent = getIntent().getStringExtra("PAGE_INTENT");
    }

    @Override
    public void onClick(View view) {

        Intent intent = new Intent();
        switch (view.getId()) {
            //返回键
            case R.id.yunqixuanze_activity_back:
                finish();
                break;
            //备孕期
            case R.id.yunqixuanze_activity_btn1:
                if (NetworkUtil.isNetworkAvailable(YunQiXuanZeActivity.this)) {
                    updateInterfaceData();
                } else {
                    ToastUtil.showToast(YunQiXuanZeActivity.this, R.string.no_network_hint);
                }

                break;
            //怀孕期
            case R.id.yunqixuanze_activity_btn2:
                /**
                 * PAGE_INTENT:1、HomeFragment;2、MineFragment(跳转页面提示);
                 * SIGN：1、continue;2、return (医院Acticity是否再次跳转提示);
                 */
                if (NetworkUtil.isNetworkAvailable(YunQiXuanZeActivity.this)) {
                    if (pageIntent != null) {
//                        intent.putExtra("SIGN", "continue");
//                        intent.putExtra("PAGE_INTENT", pageIntent);
//                        intent.setClass(this, JiuZhenActivity.class);
//                        startActivity(intent);
//                        finish();

                        intent.putExtra("PAGE_INTENT", pageIntent);
                        intent.setClass(this, YunChanSheZhiActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(this, "孕期设置失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastUtil.showToast(YunQiXuanZeActivity.this, R.string.no_network_hint);
                }
                break;
            //宝宝出生
            case R.id.yunqixuanze_activity_btn3:
                String antenatalCareHospitalId = db.select("antenatalCareHospitalId");
                if (NetworkUtil.isNetworkAvailable(YunQiXuanZeActivity.this)) {
                    if (antenatalCareHospitalId != null && !antenatalCareHospitalId.equals("") && !antenatalCareHospitalId.equals("null")) {
                        intent.setClass(this, BabyActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtil.showToast(YunQiXuanZeActivity.this, "请先选择医院");
                        intent.setClass(this, JiuZhenActivity.class);
                        intent.putExtra("SIGN", "baby");
                        startActivity(intent);
                    }
                    finish();
                } else {
                    ToastUtil.showToast(YunQiXuanZeActivity.this, R.string.no_network_hint);
                }
                break;

        }
    }

    /**
     * 备孕期设置
     */
    private void updateInterfaceData() {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    HashMap<String, String> param = new HashMap<>();
                    param.put("pregnancyStatus", 0 + "");
                    String p = new Gson().toJson(param);
                    RequestBody formBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), p);
                    Request request = new Request.Builder().url(APIMethod.YunChan_Sumit).addHeader("Authorization", db.select("token_type") + " " + db.select("access_token")).put(formBody).build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();
                    int code = response.code();
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        handler.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        handler.sendMessage(msg1);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ToastUtil.showToast(YunQiXuanZeActivity.this, "孕期设置成功");
                    if (pageIntent != null) {
//                        Intent intent = new Intent();
//                        intent.setAction("PREPARE_PREGNANCY_INFO");
//                        intent.putExtra("PAGE_INTENT", pageIntent);
//                        sendBroadcast(intent);
                        sendBroadcast(new Intent("bei_er").putExtra("bei_er", 0));
                    } else {
                        Toast.makeText(YunQiXuanZeActivity.this, "孕期设置失败", Toast.LENGTH_SHORT).show();
                    }
                    finish();
                    break;
                case 1:
                    ToastUtil.showToast(YunQiXuanZeActivity.this, "孕期设置失败");

                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
    }
}
